/** @format */

import { resultsModel } from '../../sdk'
import { recordErrorLog } from './logger'
import { CODE_TYPES } from '../../errorCode'
import { cloundAlarm } from '../../utils/common/dingTalk'
import { TRACK_EVENTS, APPKEY, APP_KEY, BASE_CONFIG } from '../../constants'
import { sendWildWoodRequest } from './wildwood'
import dayjs = require('dayjs')
/** @format */

export async function doHandler(target: Object, method: IFunction, args: any[]) {
  let ret: IResult<any>
  try {
    ret = await method.apply(target, args)
  } catch (error) {
    console.log('error', error)
    const errMessage = error instanceof Error ? error.toString() : JSON.stringify(error)
    recordErrorLog(args[0], args[1], errMessage, error.stack)
    // 云告警
    cloundAlarm(args[0], errMessage)
    // // nike告警
    // let body = {
    //   userId: args?.[0]?.openId,
    //   shopId: args?.[1]?.activityInfo?.shopId,
    //   anonymousId: args?.[1]?.userInfo?.userNick,
    //   name: 'NKPILE',
    //   event: TRACK_EVENTS.EXCEPTION,
    //   env: args?.[0]?.env === 'online' ? 'production' : 'test',
    //   type: 'track',
    //   context: {
    //     library: {
    //       name: 'segment-wildwoodmg',
    //       version: '0.0.1'
    //     },
    //     device: {
    //       type: args?.[0]?.data?.platform
    //     },
    //     app: {
    //       name: 'PILE',
    //       version: args?.[0]?.data?.version
    //     },
    //     referrer: {
    //       type: args?.[0]?.data?.isTask ? 'wildwoodmp' : 'member center'
    //     }
    //   },
    //   properties: {
    //     classification: 'experience',
    //     eventName: TRACK_EVENTS.EXCEPTION,
    //     eventType: 'track',
    //     openId: args?.[0]?.openId,
    //     timestamp: dayjs().format(),
    //     appkey: APP_KEY,
    //     tmallShopId: args?.[1]?.activityInfo?.shopId,
    //     name: 'NIKEPILE',
    //     userId: args?.[0]?.openId,
    //     traceId: args?.[0]?.data?.traceId,
    //     errorMessage: errMessage
    //   }
    // }
    // const { API_URL_LIST } = BASE_CONFIG
    // await sendWildWoodRequest(args?.[0], API_URL_LIST.DATA_TRACK, TRACK_EVENTS.EXCEPTION, body)
    ret = resultsModel.error(CODE_TYPES.SYSTEM_ERROR)
  }
  return ret
}
